home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / htmllib.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  20KB  |  626 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''HTML 2.0 parser.
  5.  
  6. See the HTML 2.0 specification:
  7. http://www.w3.org/hypertext/WWW/MarkUp/html-spec/html-spec_toc.html
  8. '''
  9. from warnings import warnpy3k
  10. warnpy3k('the htmllib module has been removed in Python 3.0', stacklevel = 2)
  11. del warnpy3k
  12. import sgmllib
  13. from formatter import AS_IS
  14. __all__ = [
  15.     'HTMLParser',
  16.     'HTMLParseError']
  17.  
  18. class HTMLParseError(sgmllib.SGMLParseError):
  19.     """Error raised when an HTML document can't be parsed."""
  20.     pass
  21.  
  22.  
  23. class HTMLParser(sgmllib.SGMLParser):
  24.     '''This is the basic HTML parser class.
  25.  
  26.     It supports all entity names required by the XHTML 1.0 Recommendation.
  27.     It also defines handlers for all HTML 2.0 and many HTML 3.0 and 3.2
  28.     elements.
  29.  
  30.     '''
  31.     from htmlentitydefs import entitydefs
  32.     
  33.     def __init__(self, formatter, verbose = 0):
  34.         '''Creates an instance of the HTMLParser class.
  35.  
  36.         The formatter parameter is the formatter instance associated with
  37.         the parser.
  38.  
  39.         '''
  40.         sgmllib.SGMLParser.__init__(self, verbose)
  41.         self.formatter = formatter
  42.  
  43.     
  44.     def error(self, message):
  45.         raise HTMLParseError(message)
  46.  
  47.     
  48.     def reset(self):
  49.         sgmllib.SGMLParser.reset(self)
  50.         self.savedata = None
  51.         self.isindex = 0
  52.         self.title = None
  53.         self.base = None
  54.         self.anchor = None
  55.         self.anchorlist = []
  56.         self.nofill = 0
  57.         self.list_stack = []
  58.  
  59.     
  60.     def handle_data(self, data):
  61.         if self.savedata is not None:
  62.             self.savedata = self.savedata + data
  63.         elif self.nofill:
  64.             self.formatter.add_literal_data(data)
  65.         else:
  66.             self.formatter.add_flowing_data(data)
  67.  
  68.     
  69.     def save_bgn(self):
  70.         '''Begins saving character data in a buffer instead of sending it
  71.         to the formatter object.
  72.  
  73.         Retrieve the stored data via the save_end() method.  Use of the
  74.         save_bgn() / save_end() pair may not be nested.
  75.  
  76.         '''
  77.         self.savedata = ''
  78.  
  79.     
  80.     def save_end(self):
  81.         '''Ends buffering character data and returns all data saved since
  82.         the preceding call to the save_bgn() method.
  83.  
  84.         If the nofill flag is false, whitespace is collapsed to single
  85.         spaces.  A call to this method without a preceding call to the
  86.         save_bgn() method will raise a TypeError exception.
  87.  
  88.         '''
  89.         data = self.savedata
  90.         self.savedata = None
  91.         if not self.nofill:
  92.             data = ' '.join(data.split())
  93.         
  94.         return data
  95.  
  96.     
  97.     def anchor_bgn(self, href, name, type):
  98.         '''This method is called at the start of an anchor region.
  99.  
  100.         The arguments correspond to the attributes of the <A> tag with
  101.         the same names.  The default implementation maintains a list of
  102.         hyperlinks (defined by the HREF attribute for <A> tags) within
  103.         the document.  The list of hyperlinks is available as the data
  104.         attribute anchorlist.
  105.  
  106.         '''
  107.         self.anchor = href
  108.         if self.anchor:
  109.             self.anchorlist.append(href)
  110.         
  111.  
  112.     
  113.     def anchor_end(self):
  114.         '''This method is called at the end of an anchor region.
  115.  
  116.         The default implementation adds a textual footnote marker using an
  117.         index into the list of hyperlinks created by the anchor_bgn()method.
  118.  
  119.         '''
  120.         if self.anchor:
  121.             self.handle_data('[%d]' % len(self.anchorlist))
  122.             self.anchor = None
  123.         
  124.  
  125.     
  126.     def handle_image(self, src, alt, *args):
  127.         '''This method is called to handle images.
  128.  
  129.         The default implementation simply passes the alt value to the
  130.         handle_data() method.
  131.  
  132.         '''
  133.         self.handle_data(alt)
  134.  
  135.     
  136.     def start_html(self, attrs):
  137.         pass
  138.  
  139.     
  140.     def end_html(self):
  141.         pass
  142.  
  143.     
  144.     def start_head(self, attrs):
  145.         pass
  146.  
  147.     
  148.     def end_head(self):
  149.         pass
  150.  
  151.     
  152.     def start_body(self, attrs):
  153.         pass
  154.  
  155.     
  156.     def end_body(self):
  157.         pass
  158.  
  159.     
  160.     def start_title(self, attrs):
  161.         self.save_bgn()
  162.  
  163.     
  164.     def end_title(self):
  165.         self.title = self.save_end()
  166.  
  167.     
  168.     def do_base(self, attrs):
  169.         for a, v in attrs:
  170.             if a == 'href':
  171.                 self.base = v
  172.                 continue
  173.         
  174.  
  175.     
  176.     def do_isindex(self, attrs):
  177.         self.isindex = 1
  178.  
  179.     
  180.     def do_link(self, attrs):
  181.         pass
  182.  
  183.     
  184.     def do_meta(self, attrs):
  185.         pass
  186.  
  187.     
  188.     def do_nextid(self, attrs):
  189.         pass
  190.  
  191.     
  192.     def start_h1(self, attrs):
  193.         self.formatter.end_paragraph(1)
  194.         self.formatter.push_font(('h1', 0, 1, 0))
  195.  
  196.     
  197.     def end_h1(self):
  198.         self.formatter.end_paragraph(1)
  199.         self.formatter.pop_font()
  200.  
  201.     
  202.     def start_h2(self, attrs):
  203.         self.formatter.end_paragraph(1)
  204.         self.formatter.push_font(('h2', 0, 1, 0))
  205.  
  206.     
  207.     def end_h2(self):
  208.         self.formatter.end_paragraph(1)
  209.         self.formatter.pop_font()
  210.  
  211.     
  212.     def start_h3(self, attrs):
  213.         self.formatter.end_paragraph(1)
  214.         self.formatter.push_font(('h3', 0, 1, 0))
  215.  
  216.     
  217.     def end_h3(self):
  218.         self.formatter.end_paragraph(1)
  219.         self.formatter.pop_font()
  220.  
  221.     
  222.     def start_h4(self, attrs):
  223.         self.formatter.end_paragraph(1)
  224.         self.formatter.push_font(('h4', 0, 1, 0))
  225.  
  226.     
  227.     def end_h4(self):
  228.         self.formatter.end_paragraph(1)
  229.         self.formatter.pop_font()
  230.  
  231.     
  232.     def start_h5(self, attrs):
  233.         self.formatter.end_paragraph(1)
  234.         self.formatter.push_font(('h5', 0, 1, 0))
  235.  
  236.     
  237.     def end_h5(self):
  238.         self.formatter.end_paragraph(1)
  239.         self.formatter.pop_font()
  240.  
  241.     
  242.     def start_h6(self, attrs):
  243.         self.formatter.end_paragraph(1)
  244.         self.formatter.push_font(('h6', 0, 1, 0))
  245.  
  246.     
  247.     def end_h6(self):
  248.         self.formatter.end_paragraph(1)
  249.         self.formatter.pop_font()
  250.  
  251.     
  252.     def do_p(self, attrs):
  253.         self.formatter.end_paragraph(1)
  254.  
  255.     
  256.     def start_pre(self, attrs):
  257.         self.formatter.end_paragraph(1)
  258.         self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
  259.         self.nofill = self.nofill + 1
  260.  
  261.     
  262.     def end_pre(self):
  263.         self.formatter.end_paragraph(1)
  264.         self.formatter.pop_font()
  265.         self.nofill = max(0, self.nofill - 1)
  266.  
  267.     
  268.     def start_xmp(self, attrs):
  269.         self.start_pre(attrs)
  270.         self.setliteral('xmp')
  271.  
  272.     
  273.     def end_xmp(self):
  274.         self.end_pre()
  275.  
  276.     
  277.     def start_listing(self, attrs):
  278.         self.start_pre(attrs)
  279.         self.setliteral('listing')
  280.  
  281.     
  282.     def end_listing(self):
  283.         self.end_pre()
  284.  
  285.     
  286.     def start_address(self, attrs):
  287.         self.formatter.end_paragraph(0)
  288.         self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
  289.  
  290.     
  291.     def end_address(self):
  292.         self.formatter.end_paragraph(0)
  293.         self.formatter.pop_font()
  294.  
  295.     
  296.     def start_blockquote(self, attrs):
  297.         self.formatter.end_paragraph(1)
  298.         self.formatter.push_margin('blockquote')
  299.  
  300.     
  301.     def end_blockquote(self):
  302.         self.formatter.end_paragraph(1)
  303.         self.formatter.pop_margin()
  304.  
  305.     
  306.     def start_ul(self, attrs):
  307.         self.formatter.end_paragraph(not (self.list_stack))
  308.         self.formatter.push_margin('ul')
  309.         self.list_stack.append([
  310.             'ul',
  311.             '*',
  312.             0])
  313.  
  314.     
  315.     def end_ul(self):
  316.         if self.list_stack:
  317.             del self.list_stack[-1]
  318.         
  319.         self.formatter.end_paragraph(not (self.list_stack))
  320.         self.formatter.pop_margin()
  321.  
  322.     
  323.     def do_li(self, attrs):
  324.         self.formatter.end_paragraph(0)
  325.         if self.list_stack:
  326.             (dummy, label, counter) = top = self.list_stack[-1]
  327.             top[2] = counter = counter + 1
  328.         else:
  329.             (label, counter) = ('*', 0)
  330.         self.formatter.add_label_data(label, counter)
  331.  
  332.     
  333.     def start_ol(self, attrs):
  334.         self.formatter.end_paragraph(not (self.list_stack))
  335.         self.formatter.push_margin('ol')
  336.         label = '1.'
  337.         for a, v in attrs:
  338.             if a == 'type':
  339.                 if len(v) == 1:
  340.                     v = v + '.'
  341.                 
  342.                 label = v
  343.                 continue
  344.         
  345.         self.list_stack.append([
  346.             'ol',
  347.             label,
  348.             0])
  349.  
  350.     
  351.     def end_ol(self):
  352.         if self.list_stack:
  353.             del self.list_stack[-1]
  354.         
  355.         self.formatter.end_paragraph(not (self.list_stack))
  356.         self.formatter.pop_margin()
  357.  
  358.     
  359.     def start_menu(self, attrs):
  360.         self.start_ul(attrs)
  361.  
  362.     
  363.     def end_menu(self):
  364.         self.end_ul()
  365.  
  366.     
  367.     def start_dir(self, attrs):
  368.         self.start_ul(attrs)
  369.  
  370.     
  371.     def end_dir(self):
  372.         self.end_ul()
  373.  
  374.     
  375.     def start_dl(self, attrs):
  376.         self.formatter.end_paragraph(1)
  377.         self.list_stack.append([
  378.             'dl',
  379.             '',
  380.             0])
  381.  
  382.     
  383.     def end_dl(self):
  384.         self.ddpop(1)
  385.         if self.list_stack:
  386.             del self.list_stack[-1]
  387.         
  388.  
  389.     
  390.     def do_dt(self, attrs):
  391.         self.ddpop()
  392.  
  393.     
  394.     def do_dd(self, attrs):
  395.         self.ddpop()
  396.         self.formatter.push_margin('dd')
  397.         self.list_stack.append([
  398.             'dd',
  399.             '',
  400.             0])
  401.  
  402.     
  403.     def ddpop(self, bl = 0):
  404.         self.formatter.end_paragraph(bl)
  405.         if self.list_stack:
  406.             if self.list_stack[-1][0] == 'dd':
  407.                 del self.list_stack[-1]
  408.                 self.formatter.pop_margin()
  409.             
  410.         
  411.  
  412.     
  413.     def start_cite(self, attrs):
  414.         self.start_i(attrs)
  415.  
  416.     
  417.     def end_cite(self):
  418.         self.end_i()
  419.  
  420.     
  421.     def start_code(self, attrs):
  422.         self.start_tt(attrs)
  423.  
  424.     
  425.     def end_code(self):
  426.         self.end_tt()
  427.  
  428.     
  429.     def start_em(self, attrs):
  430.         self.start_i(attrs)
  431.  
  432.     
  433.     def end_em(self):
  434.         self.end_i()
  435.  
  436.     
  437.     def start_kbd(self, attrs):
  438.         self.start_tt(attrs)
  439.  
  440.     
  441.     def end_kbd(self):
  442.         self.end_tt()
  443.  
  444.     
  445.     def start_samp(self, attrs):
  446.         self.start_tt(attrs)
  447.  
  448.     
  449.     def end_samp(self):
  450.         self.end_tt()
  451.  
  452.     
  453.     def start_strong(self, attrs):
  454.         self.start_b(attrs)
  455.  
  456.     
  457.     def end_strong(self):
  458.         self.end_b()
  459.  
  460.     
  461.     def start_var(self, attrs):
  462.         self.start_i(attrs)
  463.  
  464.     
  465.     def end_var(self):
  466.         self.end_i()
  467.  
  468.     
  469.     def start_i(self, attrs):
  470.         self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
  471.  
  472.     
  473.     def end_i(self):
  474.         self.formatter.pop_font()
  475.  
  476.     
  477.     def start_b(self, attrs):
  478.         self.formatter.push_font((AS_IS, AS_IS, 1, AS_IS))
  479.  
  480.     
  481.     def end_b(self):
  482.         self.formatter.pop_font()
  483.  
  484.     
  485.     def start_tt(self, attrs):
  486.         self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
  487.  
  488.     
  489.     def end_tt(self):
  490.         self.formatter.pop_font()
  491.  
  492.     
  493.     def start_a(self, attrs):
  494.         href = ''
  495.         name = ''
  496.         type = ''
  497.         for attrname, value in attrs:
  498.             value = value.strip()
  499.             if attrname == 'href':
  500.                 href = value
  501.             
  502.             if attrname == 'name':
  503.                 name = value
  504.             
  505.             if attrname == 'type':
  506.                 type = value.lower()
  507.                 continue
  508.         
  509.         self.anchor_bgn(href, name, type)
  510.  
  511.     
  512.     def end_a(self):
  513.         self.anchor_end()
  514.  
  515.     
  516.     def do_br(self, attrs):
  517.         self.formatter.add_line_break()
  518.  
  519.     
  520.     def do_hr(self, attrs):
  521.         self.formatter.add_hor_rule()
  522.  
  523.     
  524.     def do_img(self, attrs):
  525.         align = ''
  526.         alt = '(image)'
  527.         ismap = ''
  528.         src = ''
  529.         width = 0
  530.         height = 0
  531.         for attrname, value in attrs:
  532.             if attrname == 'align':
  533.                 align = value
  534.             
  535.             if attrname == 'alt':
  536.                 alt = value
  537.             
  538.             if attrname == 'ismap':
  539.                 ismap = value
  540.             
  541.             if attrname == 'src':
  542.                 src = value
  543.             
  544.             if attrname == 'width':
  545.                 
  546.                 try:
  547.                     width = int(value)
  548.                 except ValueError:
  549.                     pass
  550.                 except:
  551.                     None<EXCEPTION MATCH>ValueError
  552.                 
  553.  
  554.             None<EXCEPTION MATCH>ValueError
  555.             if attrname == 'height':
  556.                 
  557.                 try:
  558.                     height = int(value)
  559.                 except ValueError:
  560.                     pass
  561.                 except:
  562.                     None<EXCEPTION MATCH>ValueError
  563.                 
  564.  
  565.             None<EXCEPTION MATCH>ValueError
  566.         
  567.         self.handle_image(src, alt, ismap, align, width, height)
  568.  
  569.     
  570.     def do_plaintext(self, attrs):
  571.         self.start_pre(attrs)
  572.         self.setnomoretags()
  573.  
  574.     
  575.     def unknown_starttag(self, tag, attrs):
  576.         pass
  577.  
  578.     
  579.     def unknown_endtag(self, tag):
  580.         pass
  581.  
  582.  
  583.  
  584. def test(args = None):
  585.     import sys as sys
  586.     import formatter
  587.     if not args:
  588.         args = sys.argv[1:]
  589.     
  590.     if args:
  591.         pass
  592.     silent = args[0] == '-s'
  593.     if silent:
  594.         del args[0]
  595.     
  596.     if args:
  597.         file = args[0]
  598.     else:
  599.         file = 'test.html'
  600.     if file == '-':
  601.         f = sys.stdin
  602.     else:
  603.         
  604.         try:
  605.             f = open(file, 'r')
  606.         except IOError:
  607.             msg = None
  608.             print file, ':', msg
  609.             sys.exit(1)
  610.  
  611.     data = f.read()
  612.     if f is not sys.stdin:
  613.         f.close()
  614.     
  615.     if silent:
  616.         f = formatter.NullFormatter()
  617.     else:
  618.         f = formatter.AbstractFormatter(formatter.DumbWriter())
  619.     p = HTMLParser(f)
  620.     p.feed(data)
  621.     p.close()
  622.  
  623. if __name__ == '__main__':
  624.     test()
  625.  
  626.